package cn.gemiman.daoyi.module.cms.controller.admin.blogtalk;

import cn.gemiman.daoyi.framework.common.enums.UserTypeEnum;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.gemiman.daoyi.module.cms.controller.admin.blogtalk.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.blogtalk.BlogTalkDO;
import cn.gemiman.daoyi.module.cms.service.blogtalk.BlogTalkService;

@Tag(name = "管理后台 - 说说")
@RestController
@RequestMapping("/cms/blog-talk")
@Validated
public class BlogTalkController {

    @Resource
    private BlogTalkService blogTalkService;

    @PostMapping("/create")
    @Operation(summary = "创建说说")
    @PreAuthorize("@ss.hasPermission('cms:blog-talk:create')")
    public CommonResult<Long> createBlogTalk(@Valid @RequestBody BlogTalkSaveReqVO createReqVO) {
        createReqVO.setUserId(getLoginUserId()).setUserType(UserTypeEnum.ADMIN.getValue());
        return success(blogTalkService.createBlogTalk(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新说说")
    @PreAuthorize("@ss.hasPermission('cms:blog-talk:update')")
    public CommonResult<Boolean> updateBlogTalk(@Valid @RequestBody BlogTalkSaveReqVO updateReqVO) {
        blogTalkService.updateBlogTalk(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除说说")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:blog-talk:delete')")
    public CommonResult<Boolean> deleteBlogTalk(@RequestParam("id") Long id) {
        blogTalkService.deleteBlogTalk(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得说说")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:blog-talk:query')")
    public CommonResult<BlogTalkRespVO> getBlogTalk(@RequestParam("id") Long id) {
        BlogTalkDO blogTalk = blogTalkService.getBlogTalk(id);
        return success(BeanUtils.toBean(blogTalk, BlogTalkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得说说分页")
    @PreAuthorize("@ss.hasPermission('cms:blog-talk:query')")
    public CommonResult<PageResult<BlogTalkRespVO>> getBlogTalkPage(@Valid BlogTalkPageReqVO pageReqVO) {
        PageResult<BlogTalkDO> pageResult = blogTalkService.getBlogTalkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BlogTalkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出说说 Excel")
    @PreAuthorize("@ss.hasPermission('cms:blog-talk:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBlogTalkExcel(@Valid BlogTalkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BlogTalkDO> list = blogTalkService.getBlogTalkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "说说.xls", "数据", BlogTalkRespVO.class,
                        BeanUtils.toBean(list, BlogTalkRespVO.class));
    }

}
